
import time
# 实现时间戳 lts: r.ts,
import requests
from jsonpath import jsonpath

ts=int(time.time()*1000)
# print(ts)

#r.salt    = r.ts加随机0-9的数
import random
num=random.randint(0,9)
salt=str(ts)+str(num)
# print(salt)

# sign: r.sign,
import hashlib
while True:
    e=input("请输入你要翻译的内容:")

    miwen="fanyideskweb" + e + salt + "Ygy_4c=r#e#4EX^NUGUc5"
    sign=hashlib.md5(miwen.encode()).hexdigest()
    # print(sign)
    url = "https://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"
    # 构建请求头
    headers = {
        "X-Requested-With": "XMLHttpRequest",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36",
        'Cookie': 'OUTFOX_SEARCH_USER_ID_NCOO=1593310827.8658907; OUTFOX_SEARCH_USER_ID="-590911363@10.108.162.134"; DICT_UGC=be3af0da19b5c5e6aa4e17bd8d90b28a|; JSESSIONID=abcno1GMpANk7db_FX8ay; fanyi-ad-id=305566; fanyi-ad-closed=1; SESSION_FROM_COOKIE=unknown; ___rl__test__cookies=1650287148067',
        "Referer": "https://fanyi.youdao.com/"
    }
    # 3构建data参数    dict
    # 每个同学的浏览器版本不同 所以data参数在不同的位置
    # 构建参数方式  选中变量按引号
    data = {"i": e,
            "from": "AUTO",
            "to": "AUTO",
            "smartresult": "dict",
            "client": "fanyideskweb",
            "salt": salt,
            "sign": sign,
            "lts":  ts,
            "bv": "ac3968199d18b7367b2479d1f4938ac2",
            "doctype": "json",
            "version": "2.1",
            "keyfrom": "fanyi.web",
            "action": "FY_BY_REALTlME"
            }
    # 4携带data和headers发送请求
    response = requests.post(url=url, headers=headers, data=data).json()
    # print(response)
    # print(response.content.decode())
    data = jsonpath(response, "$..tgt")
    print(data[0])

# 如果说 pyinstaller不是内部或外部命令，也不是可运行的程序
# 通过pip install pyinstaller